package kr.or.common.web.taglib;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import kr.or.javacafe.member.dao.MemberDAO;
import kr.or.javacafe.member.domain.User;

public class EmpTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	
	private String type = null;
	private String empNo = null;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	

	
	
	public void writeTag(String tag) {
		try {
			pageContext.getOut().print(tag);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int doStartTag() {
		if ("empNm".equalsIgnoreCase(type)) {
			writeTag(getEmpName());
		}
		
		return SKIP_BODY;
	}

	// 아이디를 이용하여 이름을 리턴한다.
	private String getEmpName() {
		if (null != empNo) {
			ServletContext sc = ((HttpServletRequest) pageContext.getRequest()).getSession().getServletContext();

			WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc); 		
			//ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
			
			MemberDAO memberDAO = (MemberDAO)ctx.getBean("memberDAOImpl");

			User param = new User();
			param.setId(empNo);
			User user = memberDAO.selectUser(param);
			
			if (null != user) {
				return user.getName();
			} else {
				return "";
			}
		}
		
		return "";
	}
	
	
	
	

}








